﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
using System.IO;
using System.Xml.Xsl;

namespace Ghost.Tasks.Wiki
{
    public class XslTransformer
    {
        XslCompiledTransform _CompiledTransform;
        public XsltArgumentList ArgumentList { get; private set;}

        public XslTransformer(string stylesheetResourceName, string stylesheetPath) : this(stylesheetResourceName, stylesheetPath, new XsltSettings(), new XmlUrlResolver())
        {
        }
        public XslTransformer(string stylesheetResourceName, string stylesheetPath, XsltSettings settings, XmlResolver resolver)
        {            
            if (!string.IsNullOrWhiteSpace(stylesheetPath) && File.Exists(stylesheetPath))
            {
                _CompiledTransform = LoadCompiledTransformFile(stylesheetPath, settings, resolver);
            }
            else
            {
                _CompiledTransform = LoadCompiledTransformResource(stylesheetResourceName, settings, resolver);
            }
            ArgumentList = new XsltArgumentList();
        }

        public static XslCompiledTransform LoadCompiledTransformResource(string stylesheetResourceName, XsltSettings settings, XmlResolver resolver)
        {
            using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(stylesheetResourceName))
            using (XmlTextReader reader = new XmlTextReader(resourceStream))
            {
                XslCompiledTransform compiledTransform = new XslCompiledTransform();
                compiledTransform.Load(reader, settings, resolver);
                return compiledTransform;
            }
        }

        public static XslCompiledTransform LoadCompiledTransformFile(string stylesheetPath, XsltSettings settings, XmlResolver resolver)
        {
            using (XmlReader reader = XmlReader.Create(stylesheetPath))
            {
                XslCompiledTransform compiledTransform = new XslCompiledTransform();
                compiledTransform.Load(reader, settings, resolver);
                return compiledTransform;
            }
        }

        public void TransformToText(string inputPath, string outputPath)
        {
            using (XmlReader reader = XmlReader.Create(inputPath))
            using (StreamWriter writer = new StreamWriter(outputPath))
            {
                _CompiledTransform.Transform(reader, ArgumentList, writer);
            }
        }

        public void Transform(string inputPath, string outputPath)
        {
            using (XmlReader reader = XmlReader.Create(inputPath))
            using (XmlWriter writer = XmlWriter.Create(outputPath))
            {
                Transform(reader, writer);
            }
        }

        public void Transform(XmlReader reader, XmlWriter writer)
        {
            _CompiledTransform.Transform(reader, ArgumentList, writer);
        }
    }
}
